Nginx实现文件共享

前言

利用nginx的autoindex目录索引模块实现文件共享;

官方模块详解地址:https://nginx.org/en/docs/http/ngx_http_autoindex_module.html#autoindex

环境选择

CentOS 7
Nginx 1.20.2

Nginx安装

1.配置nginx的yum源文件;

[root@yejy ~]# cat /etc/yum.repos.d/Nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2.安装Nginx并设置启动与开机自启;

yum install -y nginx
systemctl start nginx && systemctl enable nginx

3.测试Nginx是否启动成功;

方法1:
#查看端口,默认端口80
[root@yejy ~]# netstat -nptl | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8200/nginx: master

方法2:
#查看进程
root@yejy ~]# ps -ef | grep nginx
root      8200     1  0 Mar27 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     8581  8200  0 Mar27 ?        00:00:00 nginx: worker process
nginx     8582  8200  0 Mar27 ?        00:00:04 nginx: worker process
root     24775 23312  0 23:40 pts/0    00:00:00 grep --color=auto nginx

方法3:
#查看Nginx状态
[root@yejy ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-03-27 20:50:55 CST; 1 day 2h ago
     Docs: http://nginx.org/en/docs/
  Process: 8195 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
  Process: 8577 ExecReload=/bin/sh -c /bin/kill -s HUP $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
  Process: 8199 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 8200 (nginx)
……

方法4:
#浏览器访问
http://127.0.0.1/
注:访问失败可尝试关闭防火墙(systemctl stop firewalld)或selinux(setenforce 0)

修改配置文件

1.进入nginx配置文件目录
cd /etc/nginx/conf.d/

2.注释默认配置文件
[root@yejy /etc/nginx/conf.d]# gzip default.conf 
[root@yejy /etc/nginx/conf.d]# ll default.conf.gz 
-rw-r--r-- 1 root root 472 Mar 27 17:50 default.conf.gz

3.修改
#创建文件共享目录
mkdir /share_data

#授权共享目录
chmod 755 -R /share_data

#修改配置文件
[root@yejy /etc/nginx/conf.d]# touch yejy.conf 
[root@yejy /etc/nginx/conf.d]# cat yejy.conf 
server {
    listen 80;
    server_name yejy1217.com;
    root /share_data/;
    autoindex on;
}

4.检查语法
[root@yejy /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5.重新加载配置文件
[root@yejy /etc/nginx/conf.d]# systemctl reload nginx

配置完成

效果查看
浏览器搜索IP,效果如下:
image

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值